package com.goods.business.service.impl;

import com.github.pagehelper.IPage;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.mapper.ProductCategoryMapper;
import com.goods.business.service.ProductCategoryService;
import com.goods.common.model.business.ProductCategory;
import com.goods.common.vo.business.ProductCategoryTreeNodeVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @description：
 * @version:
 * @author：ruixinag
 * @create：Created in 2021/11/8 14:10
 */
@Service
public class ProductCategoryServiceImpl implements ProductCategoryService {
    //设置层级
    static int lev=1;
    @Autowired
    private ProductCategoryMapper productCategoryMapper;

    //获取分页数据
    @Override
    public PageInfo<ProductCategoryTreeNodeVO> categoryTree(Map map) {
        int pageNum = Integer.parseInt(StringUtils.isEmpty(map.get("pageNum"))?"1":(String) map.get("pageNum")) ;
        int pageSize = Integer.parseInt(StringUtils.isEmpty(map.get("pageSize"))?"20":(String) map.get("pageSize")) ;


        /*
         * 获取商品的一二三级分类,并把其分类返回
         * */
        //查一级分类
        //Example o = new Example(ProductCategory.class);
        //o.createCriteria().andEqualTo("pid", 0);
        //PageHelper.startPage(pageNum, pageSize);
        //List<ProductCategory> productCategories = productCategoryMapper.selectByExample(o);
        //List<ProductCategoryTreeNodeVO> list = productCategories.stream().map(productCategory -> {
        //    ProductCategoryTreeNodeVO vo = new ProductCategoryTreeNodeVO();
        //    vo.setLev(1);
        //    BeanUtils.copyProperties(productCategory, vo);
        //    //根据一级id,查二级分类
        //    Example o1 = new Example(ProductCategory.class);
        //    o1.createCriteria().andEqualTo("pid",vo.getId());
        //    List<ProductCategory> productCategoryList2 = productCategoryMapper.selectByExample(o1);
        //    List<ProductCategoryTreeNodeVO> voList=productCategoryList2.stream().map(productCategory2->{
        //        ProductCategoryTreeNodeVO vo2 = new ProductCategoryTreeNodeVO();
        //        vo2.setLev(2);
        //        BeanUtils.copyProperties(productCategory2, vo2);
        //        //根据二级分类,查三级分类
        //        Example o2 = new Example(ProductCategory.class);
        //        o2.createCriteria().andEqualTo("pid",vo2.getId());
        //        List<ProductCategory> productCategoryList3= productCategoryMapper.selectByExample(o2);
        //        List<ProductCategoryTreeNodeVO> voList2=productCategoryList3.stream().map(productCategory3->{
        //            ProductCategoryTreeNodeVO vo3 = new ProductCategoryTreeNodeVO();
        //            vo3.setLev(3);
        //            BeanUtils.copyProperties(productCategory3,vo3);
        //            return vo3;
        //        }).collect(Collectors.toList());
        //        vo2.setChildren(voList2);
        //        return vo2;
        //    }).collect(Collectors.toList());
        //    vo.setChildren(voList);
        //    return vo;
        //}).collect(Collectors.toList());

        //为了解决分页的问题,,,总条数和总页码问题

        Page<Object> page = new Page<>(pageNum,pageSize);

        List<ProductCategoryTreeNodeVO> list = this.getProductCategoryTree(Long.parseLong("0"), lev);
        //排序
        Collections.sort(list,ProductCategoryTreeNodeVO.order());

        page.setTotal(list.size());
        //计算当前显示的数据下标起始位置
        int start=(pageNum-1)*pageSize;
        int end=Math.min(start+pageSize,list.size());
        page.addAll(list.subList(start,end));


        return new PageInfo(page);
    }

    //递归获取分类层级的数据
    private List<ProductCategoryTreeNodeVO> getProductCategoryTree(Long id,int level){
        Example o = new Example(ProductCategory.class);
        o.createCriteria().andEqualTo("pid", id);
        List<ProductCategory> productCategories = productCategoryMapper.selectByExample(o);
        if(null==productCategories){
            return null;
        }else {
           return  productCategories.stream().map(productCategory -> {
                ProductCategoryTreeNodeVO vo = new ProductCategoryTreeNodeVO();
                vo.setLev(lev);
                BeanUtils.copyProperties(productCategory, vo);
                int temp=lev;
                //根据一级id,查二级分类
                List<ProductCategoryTreeNodeVO> voList = getProductCategoryTree(vo.getId(), ++lev);
                Collections.sort(voList,ProductCategoryTreeNodeVO.order());
                //当其集合有值时,设置其子节点
                if (voList.size() > 0){
                    vo.setChildren(voList);
                }

                //把层级置为遍历初始层级
                lev=temp;
                return vo;
            }).collect(Collectors.toList());
        }

    }

    //获取父分类
    @Override
    public List<ProductCategoryTreeNodeVO> getParentCategoryTree() {
        lev=1;
        List<ProductCategoryTreeNodeVO> list = this.getProductCategoryTree(Long.parseLong("0"), lev);
        Collections.sort(list,ProductCategoryTreeNodeVO.order());
        //return productCategoryMapper.getParentCategoryTree();
        return list;
    }

    //添加分类
    @Override
    public void add(ProductCategory productCategory) {
         productCategoryMapper.insert(productCategory);
    }

    //删除分类信息,把其分类下的信息也删除
    @Override
    public boolean deleteCategoryById(Long id) {
        List<ProductCategoryTreeNodeVO> productCategoryTree = getProductCategoryTree(id, lev);
        if(productCategoryTree.size()>0){
            //有子分类,不能删除
            return false;
        }
        productCategoryMapper.deleteByPrimaryKey(id);
        return true;
    }

    //根据id获取分类信息
    @Override
    public ProductCategory editCategoryById(Long id) {

        return productCategoryMapper.selectByPrimaryKey(id);
    }

    //更新分类信息
    @Override
    public void updateCategory(ProductCategory productCategory) {
        productCategory.setModifiedTime(new Date());
        productCategoryMapper.updateByPrimaryKey(productCategory);
    }

}
